package com.smartao.base.config;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.core.mvc.BaseAction;
import com.smartao.core.mvc.ResultStatus;
import com.smartao.core.security.LoginConfig;

@Controller
@Scope("prototype")
@Results({ @Result(name = BaseAction.SUCCESS, location = "security.jsp"),
	@Result(name = BaseAction.JSON, type="json",params={"root","resultStatus"})})
public class SecurityConfigAction extends BaseAction {

	private static final long serialVersionUID = 1144337108711742847L;

	@Autowired
	private ConfigService configService;
	
	private LoginConfig login;
	private PasswordConfig password;

	@Override
	public String execute() throws Exception{
		login = new LoginConfig().loadConfig();
		password = new PasswordConfig().loadConfig();
		return SUCCESS;
	}
	
	public String save() throws Exception{
		try {
			configService.saveConfig(login);
			configService.saveConfig(password);
			resultStatus = new ResultStatus(true, "设置已保存");
		} catch (Exception e) {
			resultStatus = new ResultStatus(false, "保存出错!");
			logger.error(e.getMessage(),e);
		}
		return JSON;
	}

	public PasswordConfig getPassword() {
		return password;
	}

	public void setPassword(PasswordConfig password) {
		this.password = password;
	}

	public LoginConfig getLogin() {
		return login;
	}

	public void setLogin(LoginConfig login) {
		this.login = login;
	}

}
